<template>
  <div>
      <p>修改密码</p>
      <el-form :model="userpassword" :rules="userinfoRules" ref="changepasswordRef" label-position="left">
        <el-form-item label="请输入原密码" :label-width="formLabelWidth" prop="oldPwd">
          <el-input v-model="userpassword.oldPwd" autocomplete="off" type="password" placeholder="请输入原密码"></el-input>
        </el-form-item>
        <el-form-item label="请输入新密码" :label-width="formLabelWidth" prop="newPwd">
          <el-input v-model="userpassword.newPwd" autocomplete="off" type="password" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="请再次确认新密码" :label-width="formLabelWidth" prop="renewPwd">
          <el-input v-model="userpassword.renewPwd" autocomplete="off" type="password" placeholder="请确认新密码"></el-input>
        </el-form-item>
      </el-form>
      <div class="btnplace">
        <el-button @click="resetForm" size="small">重 置</el-button>
        <el-button @click="confimFn" type="primary" size="small">确 定</el-button>
      </div>
  </div>
</template>

<script>
import { changepasswordAPI } from '@/api'
export default {
  name: 'admin-changepassword',
  data () {
    const samePwdFN = (rule, value, callback) => {
      if (value !== this.userpassword.newPwd) {
        // 若验证失败，则调用回调函数时指定一个error对象
        callback(new Error('两次密码输入不一致。请重新输入'))
      } else {
        // 若验证成功，则直接调用回调函数即可
        callback()
      }
    }
    return {
      formLabelWidth: '140px',
      userpassword: {
        oldPwd: '',
        newPwd: '',
        renewPwd: ''
      },
      userinfoRules: {
        oldPwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须为6-15位的非空字符', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须为6-15位的非空字符', trigger: 'blur' }
        ],
        renewPwd: [
          { required: true, message: '请再次输入新密码', trigger: 'blur' },
          { validator: samePwdFN, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetForm () {
      this.$refs.changepasswordRef.resetFields()
    },
    // 确认修改
    confimFn () {
      this.$refs.changepasswordRef.validate(async valid => {
        if (valid) {
          const { data } = await changepasswordAPI(this.userpassword)
          if (data.status !== 0) return this.$message.error(data.message)
          this.$message.success(data.message)
          this.$refs.changepasswordRef.resetFields()
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
   .addcate{
    margin-bottom: 20px;
    display: flex;
    justify-content: space-between;
   }
   .el-input{
    width:300px;
   }
  .el-button {
    border-radius: 8px;
  }
  .btnplace {
    margin-left: 50px;
  }

  p {
    font-size: large;
    text-align: left;
  }
  ::v-deep .el-input__inner{
    height: 2.5em;
    border-radius: 14px;
  }
</style>
